package com.teamwings.common.aspect;

import com.teamwings.common.aspect.annotation.FormControl;
import com.teamwings.common.aspect.annotation.FormControlScan;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Slf4j
public class FormControlScannerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {

    ResourceLoader resourceLoader;

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata,
                                        BeanDefinitionRegistry beanDefinitionRegistry) {

        //获取所有注解的属性和值
        AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(annotationMetadata.getAnnotationAttributes(FormControlScan.class.getName()));
        //获取到basePackage的值
        String[] basePackages = new String[0];
        if (annoAttrs != null) {
            basePackages = annoAttrs.getStringArray("basePackage");
        }
        //如果没有设置basePackage 扫描路径,就扫描对应包下面的值
        if (basePackages.length == 0) {
            basePackages = new String[]{((StandardAnnotationMetadata) annotationMetadata).getIntrospectedClass().getPackage().getName()};
        }

        //自定义的包扫描器
        FindFormControlClassPathScanHandle scanHandle = new FindFormControlClassPathScanHandle(beanDefinitionRegistry, false);

        if (resourceLoader != null) {
            scanHandle.setResourceLoader(resourceLoader);
        }

        //扫描指定路径下的接口
        Set<BeanDefinitionHolder> beanDefinitionHolders = scanHandle.doScan(basePackages);

        Map<String, String> typeClassMap = new HashMap<>();
        Map<String, String> layoutTypeClassMap = new HashMap<>();
        Map<String, String> noFieldClassMap = new HashMap<>();

        for (BeanDefinitionHolder beanDefinitionHolder : beanDefinitionHolders) {
            BeanDefinition beanDefinition = beanDefinitionHolder.getBeanDefinition();
            String beanClassName = beanDefinition.getBeanClassName();
            try {
                Class<?> aClass = Class.forName(beanClassName);
                FormControl annotation = aClass.getAnnotation(FormControl.class);
                log.info("扫描组件: " + annotation.type());
                if (annotation.needField()) {
                    log.info("组件类型: " + (annotation.isLayout() ? "布局组件" : "普通组件"));
                    if (annotation.isLayout()) {
                        layoutTypeClassMap.put(annotation.type(), beanClassName);
                    } else {
                        typeClassMap.put(annotation.type(), beanClassName);
                    }
                } else {
                    log.info("组件类型: 不需要创建数据库字段组件");
                    noFieldClassMap.put(annotation.type(), beanClassName);
                }
                log.info(annotation.type() + "组件的classname: " + beanClassName);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        FormControlMap.typeClassMap.putAll(typeClassMap);
        FormControlMap.layoutTypeClassMap.putAll(layoutTypeClassMap);
        FormControlMap.noFieldClassMap.putAll(noFieldClassMap);
    }
}
